package cn.silver.framework.mybatis.controller;

import cn.silver.framework.core.validate.QueryGroup;
import cn.silver.framework.log.annotation.Log;
import cn.silver.framework.log.enums.OperateEnums;
import cn.silver.framework.mybatis.bean.BusBean;
import cn.silver.framework.mybatis.bean.PageModel;
import cn.silver.framework.mybatis.domain.BusEntity;
import cn.silver.framework.mybatis.param.BusParam;
import cn.silver.framework.mybatis.service.IBusService;
import cn.silver.framework.openapi.constant.ResultEnum;
import cn.silver.framework.openapi.model.Result;
import cn.silver.framework.openapi.model.ResultPage;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * @author zhouxiaobo
 */
@Slf4j
public abstract class BusController<S extends IBusService<T, P, V>, T extends BusEntity, P extends BusParam, V extends BusBean>
        extends BaseController<S, T, P, V> {

    /**
     * 查询回收站列表
     *
     * @param param 查询条件
     * @return
     */
    @GetMapping("/recycle")
    @Log(operate = OperateEnums.SEAR_RECYCLE)
    @Operation(summary = "查询逻辑删除数据", description = "分页获取逻辑删除数据")
    public ResultPage<V> recycle(@Validated(QueryGroup.class) @ModelAttribute P param) {
        this.checkPermission("list");
        PageModel page = this.getPageInfo();
        if (StringUtils.isBlank(page.getOrderByColumn())) {
            page.setOrderByColumn(param.getOrderColumn());
        }
        if (StringUtils.isBlank(page.getIsAsc())) {
            page.setIsAsc(param.getOrderType());
        }
        return this.baseService.selectRecycle(page, param);
    }

    /**
     * 数据重复性校验
     *
     * @param param 校验对象
     */
    @GetMapping("/exists")
    @Log(operate = OperateEnums.ALTER_CHECK)
    @Operation(summary = "数据重复性校验", description = "数据重复性校验")
    public Result<Void> exists(@ModelAttribute P param) {
        this.checkPermission("list");
        long count = this.baseService.selectCount(param);
        if (count > 0) {
            return Result.fail(ResultEnum.DATA_ERROR_EXIST);
        }
        return Result.ok("数据可用");
    }

    /**
     * 彻底删除数据
     *
     * @param id 数据主键
     */
    @DeleteMapping("/remove/{id}")
    @Log(operate = OperateEnums.ALTER_REMOVE)
    @Operation(summary = "彻底删除数据", description = "根据Id彻底删除一条数据")
    public Result<Void> remove(@PathVariable String id) {
        this.checkPermission("remove");
        int success = this.baseService.remove(id);
        return Result.ok("数据删除成功,共删除" + success + "条数据");
    }

    /**
     * 彻底删除数据
     *
     * @param ids 数据主键
     */
    @DeleteMapping("/remove/batch/{ids}")
    @Log(operate = OperateEnums.ALTER_REMOVE)
    @Operation(summary = "批量彻底删除数据", description = "根据Id批量彻底删除数据")
    public Result<Void> removeBatch(@PathVariable String[] ids) {
        this.checkPermission("remove");
        int success = this.baseService.removeBatch(Arrays.asList(ids));
        return Result.ok("数据删除成功,共删除" + success + "条数据");
    }

    /**
     * 还原逻辑删除数据
     *
     * @param id 数据主键
     */
    @PostMapping("/restore/{id}")
    @Log(operate = OperateEnums.ALTER_RESTORE)
    @Operation(summary = "还原已删除数据", description = "根据Id还原一条已删除数据")
    public Result<Void> restore(@PathVariable String id) {
        this.checkPermission("remove");
        int success = this.baseService.restore(id);
        return Result.ok("数据还原成功,共还原" + success + "条数据");
    }

    /**
     * 还原逻辑删除数据
     *
     * @param ids 数据主键
     */
    @PostMapping("/restore/batch/{ids}")
    @Log(operate = OperateEnums.ALTER_RESTORE)
    @Operation(summary = "批量还原已删除数据", description = "根据Id批量还原已删除数据")
    public Result<Void> restoreBatch(@PathVariable String[] ids) {
        this.checkPermission("remove");
        int success = this.baseService.restoreBatch(Arrays.asList(ids));
        return Result.ok("数据还原成功,共还原" + success + "条数据");
    }
}
